#!/bin/bash
# 扩展模块初始化数据
if [ ! -n "${1}" ] || [ ! -n "${2}" ] || [ ! -n "${3}" ] || [ ! -n "${4}" ] || [ ! -n "${5}" ];
    then
    echo "ERROR! install extends: param error, request rootPassword,schoolCode,version,modules,sections, your param is : $*"
    exit 0;
fi
rootPassword=${1}
schoolCode=${2}
version=${3}
modules=${4}
sections=${5}

log="/var/log/mysql/mylog/install.log"
if [ ! -f "$log" ]; then
        mkdir -p /var/log/mysql/mylog
        touch $log
fi

dataPath=/usr/local/data
rm -rf /usr/local/install
cp -r /usr/local/data /usr/local/install
if [ ! -d "$dataPath" ]; then
        echo "ERROR! $dataPath is not exist."
        exit -1;
fi

if [ ! -d "/usr/local/install/init/user-mysql/$version" ]; then
        mkdir -p /usr/local/install/init/user-mysql/$version
fi
moduleList=${modules//,/ }
for module in $moduleList; do
	echo "$(date +%Y%m%d%k%M%S) init $module data begin." >> $log
	sed -i "s/{schoolcode}/$schoolCode/g" /usr/local/install/schoolset/schoolset-$module.sql
	sh /usr/local/install/init-$module.sh $rootPassword $version $sections &> /var/log/mysql/mylog/init-$module.log
	mysql -uroot -p$rootPassword --default-character-set=utf8 < /usr/local/install/schoolset/schoolset-$module.sql &> /var/log/mysql/mylog/init-$module-schoolset.log
done
errorlog=`cat  /var/log/mysql/mylog/init-$module.log|grep 'ERROR'`
if [ ! -z "$errorlog" ]; then
    echo $errorlog
    exit -1;
fi
echo "SUCCESS"
